package servlet;

import dao.UsuarioDao;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Usuario;

public class CadastroUsuariosServlet extends HttpServlet {

	private ServletConfig config;

	@Override
	public void init(ServletConfig config) throws ServletException {
		this.config = config;
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 1a. Recuperar os dados oriundos do FORM
		String nome = request.getParameter("nome");
		String email = request.getParameter("email");
		String senha = request.getParameter("senha");
		
		// 2a. Validar os dados recuperados
		RequestDispatcher disp = request.getRequestDispatcher("erro.html");
		
                Usuario u = new Usuario();
                u.setNome(nome);
                u.setEmail(email);
                u.setSenha(senha);
                
                UsuarioDao uD = new UsuarioDao();
                Boolean retorno = uD.insere(u);
                
                if(retorno){
                    disp = request.getRequestDispatcher("login.jsp");
                    disp.forward(request, response);
                }
                
                
                /*
                if (nome == null || nome.trim().length() == 0) {
			disp.forward(request, response);
			return;
		}
                
		if (email == null || email.trim().length() == 0) {
			disp.forward(request, response);
			return;
		}

		if (senha == null || senha.trim().length() != 6) {
			disp.forward(request, response);
			return;
		}
                
		*/
		

	}

}
